package com.kpx.app.adapter;

import android.content.Context;
import android.view.View;

import com.kpx.app.R;
import com.kpx.app.activity.ActivityPayList;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.PayInfoBean;
import com.kpx.app.utils.UserInfoUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by congdy on 17/4/15.
 */

public class PayAdapter extends ComAdapter {
    private int lastPosition = 0;
    private final double wallet;

    /**
     * 构造方法
     *
     * @param context  上下文对象
     * @param datas    数据
     * @param layoutId
     * @version 1.0
     * @createTime 2015年5月24日, 上午11:47:30
     * @updateTime 2015年5月24日, 上午11:47:30
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public PayAdapter(Context context, List datas, int layoutId) {
        super(context, datas, layoutId);
        wallet = UserInfoUtils.getCachedLoginUser().getWALLET().getCashAmount();
    }

    @Override
    public void convert(ComViewHolder holder, Object o, final int position) {
        final PayInfoBean b = (PayInfoBean) o;
        holder.setVisibility(R.id.cb_pay, b.getPayCode() == 10 ? ComViewHolder.GONE : ComViewHolder.VISIBLE)
                .setVisibility(R.id.img_wallet, b.getPayCode() == 10 ? ComViewHolder.VISIBLE : ComViewHolder.GONE);
        holder.setText(R.id.txt_pay, b.getPayName())
                .setText(R.id.txt_pay_price, "")
                .setCBchecked(R.id.cb_pay, b.ischecked())
                .getConvertVeiw().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (b.getPayCode() == 10) return;
                if (lastPosition == position) {
                    return;
                } else {
                    b.setIschecked(true);
                    ActivityPayList.selectPay = b;
                    ((PayInfoBean) mDatas.get(lastPosition)).setIschecked(false);
                    notifyDataSetChanged();
                    lastPosition = position;
                }
            }
        });

        holder.setText(R.id.txt_pay_price, b.ischecked() ?(ActivityPayList.relprice<=wallet?"0.00": df.format(ActivityPayList.relprice-wallet)+""):"");
        if (b.getPayCode() == 1) {//支付宝
            holder.setImageResource_bg(R.id.img_icon, R.drawable.payment_mode_head_image_alipay);
        } else if (b.getPayCode() == 3) {//微信
            holder.setImageResource_bg(R.id.img_icon, R.drawable.payment_mode_head_image_wechat);
        } else if (b.getPayCode() == 10) {//微信
            holder.setImageResource_bg(R.id.img_icon, R.drawable.pay_wallet_icon);
            holder.setText(R.id.txt_pay_price, df.format(wallet) + "");
        }
    }
    DecimalFormat df = new DecimalFormat("###0.00");

}
